<?php
    /**
     * Created by PhpStorm.
     * User: huangzhongwei
     * Date: 2018/10/29
     * Time: 11:22
     */

    namespace app\api\command;


    use app\common\model\AccountHasVoucher;
    use think\console\Command;
    use think\console\Input;
    use think\console\Output;
    use think\facade\Log;
    use app\common\model\Order;


    class UpOrder extends Command
    {
        protected function configure()
        {
            $this->setName('UpOrder')->setDescription("更新订单");
        }

        protected function execute(Input $input, Output $output)
        {
            //订单 15分钟 未支付 修改订单  优惠卷状态改变


            //获取超时未支付订单

            $noPayOrder = Order::where('expire_time', '<', time())->where('state', 1)->select();
            $orderUpdateData = [];
            $voucherUpdateData=[];
            foreach ($noPayOrder as $val) {
                $array          = [];
                $voucher=[];
                $array['id']    = $val['id'];
                $array['state'] = 6;
                if($val['account_has_voucher_id']){
                    $voucher['id']=$val['account_has_voucher_id'];
                    $voucher['is_used']=0;
                }
                array_push($orderUpdateData, $array);
                array_push($voucherUpdateData,$voucher);

            }
            $order = new Order();

            $order->saveAll($orderUpdateData);

            (new AccountHasVoucher())->saveAll($voucherUpdateData);

            Log::record();
        }

    }